Odoo / Model / Form Validation using model
Form Validation
-
Step 1:
import re # for matching class Student(models.Model): _name = "student" name = fields.Char(string='Name', required=True) nid = fields.Char(string='NID', required=True) @api.constrains('name') def check_name(self): """ make sure name 10-15 alphabets and spaces""" for rec in self: # here i forced that the name should start with alphabets if it's not the case remove ^[a-zA-Z] # and just keep: re.match(r"[ a-zA-Z]+", rec.name) if not 10 <= len(rec.name) <= 15 or not re.match(r"^[a-zA-Z][ a-zA-Z]*", rec.name): raise exceptions.ValidationError(_('your message about 10-15 alphabets and spaces')) @api.constrains('nid') def check_nid(self): """ make sure nid starts with capital letter, followed by 12 numbers and ends with a capital letter""" for rec in self: if not re.match(r"^[A-Z][0-9]{12}[A-Z]$", rec.nid): raise exceptions.ValidationError(_('your message about capital letter, followed' 'by 12 numbers and ends with a capital letter')